package app1.rapidsquare.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import app1.rapidsquare.com.activity.R;

/**
 * Created by Hikmat on 11.08.2014.
 */
public class SideNavigationAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
    private ArrayList<String> mSideNavigationItems;
    private LayoutInflater mInflater;
    private int mSelectedItemPosition = 0;
    private Context mContext;

    public SideNavigationAdapter(Context context, ArrayList<String> items) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mSideNavigationItems = items;
    }

    @Override
    public int getCount() {
        return mSideNavigationItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mSideNavigationItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.adapter_side_navigation,
                    null);
            holder = new ViewHolder();
            holder.text = (TextView) view
                    .findViewById(R.id.textViewSideNavigation);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        String item = mSideNavigationItems.get(position);
        holder.text.setText(item);
        if (mSelectedItemPosition == position)
            holder.text.setTextColor(mContext.getResources().getColor(R.color.side_navigation_selected_color));
        else
            holder.text
                    .setTextColor(mContext.getResources().getColor(R.color.side_navigation_color));
        notifyDataSetChanged();
        return view;
    }

    class ViewHolder {
        TextView text;
    }

    public void setSelectedItemPosition(int position) {
        this.mSelectedItemPosition = position;
        notifyDataSetChanged();
    }

    public String getSelectedItem() {
        return getItem(this.mSelectedItemPosition).toString();
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {
        this.mSelectedItemPosition = position;
        notifyDataSetChanged();
    }
}
